﻿namespace MyMehtodExtension
{
    public static class MethodExtensionNew
    {
        /// <summary>
        /// 扩展了一个String字符串，支持了自动的长度截取；
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string StringLength(this string resource, int length = 10)
        {
            if (string.IsNullOrWhiteSpace(resource))
            {
                return string.Empty;
            }
            else if (resource.Length <= length)
            {
                return resource;
            }
            else
            {
                //字符串必然大于10
                return $"{resource.Substring(0, length)}...";
            }
        }

        /// <summary>
        /// 扩展Object类型
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string ObjectLength(this object resource, int length = 10)
        {
            if (resource is string)
            {
                return resource.ToString().StringLength();
            }
            else if (resource is Guid)
            {
                return resource.ToString();
            }
            else
            {
                return resource.ToString();
                //
            }
        }

        /// <summary>
        /// 给泛型扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="resource"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string ExtGeneric<T>(this T resource, int length = 10) where T : class
        {
            if (resource is string)
            {
                return resource.ToString().StringLength();
            }
            else if (resource is Guid)
            {
                return resource.ToString();
            }
            else
            {
                return resource.ToString();
                //
            }
        }
    }
}